package com.film.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.domain.User;

@WebFilter("/*")
public class LoginFilter extends HttpFilter{

	@Override
	protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		//创建一个数组来记录需要拦截的资源
		String[] uriArray = {"/review.do","/reviewPriase.do","/clearReviewPriase.do",
				"/createOrder.do","/order.do","/pay.do","/currentOrder","/removeOrder.do",
				"/selectSeat.do","seat.jsp","pay.jsp","order.jsp","userinfor.jsp",
				"/showOrderDetails.do","orderDetails.jsp"};
		//获取当前请求的uri
		String uri = request.getRequestURI();
		boolean flag = false;
		for (int i = 0; i < uriArray.length; i++) {
			if(uri.contains(uriArray[i])) {
				flag = true;
				break;
			}
		}
		if(flag) {
			HttpSession session = request.getSession();
			User user = (User)session.getAttribute("userLogin");
			if(user != null) {
				chain.doFilter(request, response);
				return;
			}else {
				response.sendRedirect("login.jsp");
				return;
			}
		}else {
			chain.doFilter(request, response);
			return;
		}
	}
}
